package com.hellpro.lolbox.video.view.fragment.view;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.utils.UIManger;
import com.hellpro.lolbox.video.bean.VideoHotBean;
import com.hellpro.lolbox.video.model.VideoHotLvAdapter;
import com.hellpro.lolbox.video.presenter.VideoPresenter;
import com.hellpro.lolbox.video.view.VideoWebActivity;
import com.hellpro.lolbox.video.view.fragment.IVideoHotFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/19
 * 备注:视频热榜
 */
public class HotFragment extends BaseFragment implements IVideoHotFragment {


    @BindView(R.id.video_hot_lv)
    PullToRefreshListView videoHotLv;
    private VideoPresenter presenter;
    private VideoHotLvAdapter videoHotLvAdapter;
    private int page = 1;
    private List<VideoHotBean> data;
   private List<VideoHotBean> mList = new ArrayList<>();


    @Override
    public void initView() {
        presenter = new VideoPresenter(this, getActivity(), mHandler);
    }


    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/20
     * 备注:利用Handler进行传值
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                data = (List<VideoHotBean>) msg.obj;
                mList.addAll(data);
                videoHotLvAdapter.setData(mList);
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.setVideoHotInfo();
        videoHotLvAdapter = new VideoHotLvAdapter();
        videoHotLv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        videoHotLv.setAdapter(videoHotLvAdapter);
        videoHotLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                page += 1;
                if (page < 50) {
                    presenter.setVideoHotInfo();
                    videoHotLv.onRefreshComplete();
                }
            }
        });

        videoHotLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UIManger.startToWebActivity(getActivity(), VideoWebActivity.class,mList.get(position).getVideo_url());
            }
        });

    }

    @Override
    public String getUrl() {
        return UrlConfigs.setVideoHotUrl(page);
    }

    @Override
    public PullToRefreshListView getRefreshLv() {
        return videoHotLv;
    }

    @Override
    public void startLoadHotInfo() {

    }

    @Override
    public int getPAge() {
        return page;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
